
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ViewFrameMain extends AbstractView {
	
	public static final Integer FRAME_PIXEL_WIDTH = 800;
	public static final Integer FRAME_PIXEL_HEIHGT = 700;
	public static final Integer PANEL_PIXEL_WIDTH = 794;
	public static final Integer PANEL_PIXEL_HEIGHT = 672;

	private JFrame frameMain;
	private ArrayList<JPanel> panelsList;
	private AbstractController controller;
	
	/*
	 * Constructors
	 */
	public ViewFrameMain() {		
		// Create a frame
		frameMain = new JFrame();
		frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameMain.setBounds(100, 100, FRAME_PIXEL_WIDTH, FRAME_PIXEL_HEIHGT);
		frameMain.setResizable(false);
		frameMain.setVisible(true);	
	}
	
	public void addPanel(JPanel panel) {
		if(panelsList == null) {
			panelsList = new ArrayList<JPanel>();
		}
		
		panelsList.add(panel);
	}
	
	/*
	 * Accessors
	 */
	public JFrame getFrame() {
		return frameMain;
	}
	
	public AbstractController getController() {
		return controller;
	}
	
	/*
	 * Mutators
	 */
	public void setController(AbstractController ctrl) {
		this.controller = ctrl;
	}
	
	/*
	 * Event handler for those events coming from the controller
	 */
	public void modelPropertyChange(final PropertyChangeEvent evt) {
		System.out.println(this.getClass().toString() + ".modelPropertyChange() called -- " + 
				evt.getPropertyName() + ": " + evt.getOldValue().toString() + 
				" -> " + evt.getNewValue().toString());
		try {
			if (evt.getPropertyName().equals("panelID")) {
				frameMain.setContentPane(panelsList.get(Integer.parseInt(evt.getNewValue().toString())));
				frameMain.getContentPane().requestFocusInWindow();
				frameMain.setVisible(true);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
